### Date: 2023-04-27
### Work: is Transparency the New Political Judgment?
### Author: Joel Martinsson
### Data: Politikerpanelen 
### Data reference: Martinsson, J., Andreasson, M., Andersson, F., 2021, LORE Field Report PP15 – 2020, Gothenburg University of Gothenburg, LORE.
### Other Information: N/A
####################################################################################################################################

# Library 
library(tidyverse)
library(here)
library(haven)
library(ggrepel)
library(socviz)
library(naniar)
library(readxl)
library(scales)
library(gridExtra)
library(wrapr)
library(RColorBrewer)
library(ggpubr)
library(effsize)

# Data import
library(readxl)
df <- read_excel("Documents/Articles/Is Political Judgment the New Transparency?/codingdata_20231009.xlsx")
View(codingdata_20231009)

# Rename Variables 
df_rn <- df %>%
  rename(experiment_group = expgr,
         submit_proposal = Q24_lagger_forslag_troligt,
         likely_no_mention = Troligt_ejomnämnd,
         likely_mention = Troligt_omnämnd,
         occurs_general_likely = Q26_vanligt_forekommande,
         occured_personal = Q28_egen_erfarenhet,
         ethics_one = Q30_etik_1,
         ethics_two = Q30_etik_2,
         open_question = Q32,
         experience = erfarenhet)

# Note for replication: The primary variables in the analysis are "Submit proposal" and "Experiment group",where those taking the value one was in
# the transparent conditions, and those in the untransparent conditions taking the value 2. The other variables are not on concern for this paper,
# but is left in to ensure the full transparency of the data set. Descirptions of these can be found in the data-set.

### Missing values Replacement
df_rn$submit_proposal <- replace(df_rn$submit_proposal, df_rn$submit_proposal == -99, NA)
df_rn$likely_no_mention <- replace(df_rn$likely_no_mention, df_rn$likely_no_mention == -99, NA)
df_rn$open_question <- replace(df_rn$open_question, df_rn$open_question == -99, NA)
df_rn$occurs_general_likely <- replace(df_rn$occurs_general_likely, df_rn$occurs_general_likely == -99, NA)
df_rn$occured_personal <- replace(df_rn$occured_personal, df_rn$occured_personal == -99, NA)

#########################################################################################################

### Chi-Square test of Association Positive aspects

# Possitive Conditional
contingency_table_pc <- xtabs(~ experiment_group + possitive_conditional, data=df_rn)
print(contingency_table_pc)

chi2_test_no_yates <- chisq.test(contingency_table_pc, correct=FALSE)
print(chi2_test_no_yates)

# Possitive Expertise
contingency_table_ep <- xtabs(~ experiment_group + possitive_expertise, data=df_rn)
print(contingency_table_ep)

chi2_test_no_yates <- chisq.test(contingency_table_ep, correct=FALSE)
print(chi2_test_no_yates)

# Possitive Inspiration
contingency_table_ip <- xtabs(~ experiment_group + possitive_inspiration, data=df_rn)
print(contingency_table_ip)

chi2_test_no_yates <- chisq.test(contingency_table_ip, correct=FALSE)
print(chi2_test_no_yates)

# Possitive practical
contingency_table_pr <- xtabs(~ experiment_group + possitive_practical, data=df_rn)
print(contingency_table_pr)

chi2_test_no_yates <- chisq.test(contingency_table_pr, correct=FALSE)
print(chi2_test_no_yates)

# Test with Yates correction
chi2_test <- chisq.test(contingency_table_pc)
print(chi2_test)
chi2_test <- chisq.test(contingency_table_ep)
print(chi2_test)
chi2_test <- chisq.test(contingency_table_ip)
print(chi2_test)
chi2_test <- chisq.test(contingency_table_pr)
print(chi2_test)

### Chi-Square test of Association Negative aspects

# Negative undemocratic
contingency_table_ud <- xtabs(~ experiment_group + negative_democracy, data=df_rn)
print(contingency_table_ud)

chi2_test_no_yates <- chisq.test(contingency_table_ud, correct=FALSE)
print(chi2_test_no_yates)

# Negative Lack of Judgment
contingency_table_ju <- xtabs(~ experiment_group + negative_judgment, data=df_rn)
print(contingency_table_ju)

chi2_test_no_yates <- chisq.test(contingency_table_ju, correct=FALSE)
print(chi2_test_no_yates)

# Negative Lack of Integrity 
contingency_table_ip <- xtabs(~ experiment_group + possitive_inspiration, data=df_rn)
print(contingency_table_ip)

chi2_test_no_yates <- chisq.test(contingency_table_ip, correct=FALSE)
print(chi2_test_no_yates)

# Negative Transparency
contingency_table_tr <- xtabs(~ experiment_group + negative_transparency, data=df_rn)
print(contingency_table_tr)

chi2_test_no_yates <- chisq.test(contingency_table_tr, correct=FALSE)
print(chi2_test_no_yates)

# Test with Yates correction
chi2_test <- chisq.test(contingency_table_ud)
print(chi2_test)
chi2_test <- chisq.test(contingency_table_ip)
print(chi2_test)
chi2_test <- chisq.test(contingency_table_ju)
print(chi2_test)
chi2_test <- chisq.test(contingency_table_tr)
print(chi2_test)

#########################################################################################################

## T-test (Welch) & Cohen's D
t_test_result <- t.test(submit_proposal ~ experiment_group, data=df_rn)
print(t_test_result)

cohen_d_result <- cohen.d(submit_proposal ~ experiment_group, data=df_rn)
print(cohen_d_result)

#########################################################################################################

######## Violin Plot 

## reshape data
df_rn$experiment_group <- factor(df_rn$experiment_group)
df_rn$submit_proposal <- as.numeric(df_rn$submit_proposal)

### Violin Plot Done (with stars)
ggplot(df_rn, aes(x = experiment_group, y = submit_proposal, fill = experiment_group)) +
  geom_violin(data = subset(df_rn, !is.na(experiment_group)), scale = "width", alpha = 0.7) +
  labs(title = "Likelihood to Submit Proposal", y = "Likelihood Submit Proposal") +
  scale_fill_brewer(palette = "Dark2", guide = "none") +
  theme_minimal() +
  scale_x_discrete(labels = c("Untransparent", "Transparent")) +  # Change label order here
  scale_y_continuous(breaks = seq(1, 7, 1)) +
  theme(
    axis.title.x = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 10, face = "bold"),
    axis.title.y = element_text(size = 10),
    axis.text = element_text(size = 10)
  ) +
  stat_compare_means(label = "p.signif", size = 4, symnum.args = list(cutpoints = c(0, 0.05, 1),
                                                                      symbols = c("***", " ")))
# Saving the plot
ggsave("plot_done.png", plot = plot_done, width = 10, height = 7)
